package com.epoch.servicecourse.service.impl;

import bean.response.ResponseBean;
import bean.response.ResultEnum;
import com.epoch.servicecourse.bean.TeacherCourse;
import com.epoch.servicecourse.dao.*;
import com.epoch.servicecourse.service.TeacherCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.ResultInfoUtil;

import java.util.*;

/**
 * @author admin
 */
@Service
@Slf4j
public class TeacherCourseServiceImpl implements TeacherCourseService {

    @Autowired
    private TeacherCourseDao teacherCourseDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private TypeDao typeDao;

    @Override
    public ResponseBean getCourseList() {
        List<TeacherCourse> courseList = teacherCourseDao.getCourseList();
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"获取课程列表成功",dataResult(courseList));
    }

    /**
     * 通过课程类型获取课程
     * @param typeContent
     * @return
     */
    @Override
    public ResponseBean getCourseListByTypeContent(String typeContent) {
        List<TeacherCourse> courseList = teacherCourseDao.getCourseListByTypeContent(typeContent);
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"通过类型获取课程列表成功",dataResult(courseList));
    }

    @Override
    public ResponseBean getCourseByCourseId(String courseId) {
        if (teacherCourseDao.getCourseByCourseId(courseId) == null){
            //课程不存在
            return ResultInfoUtil.buildError(ResultEnum.BAD_REQUEST, "获取课程失败，课程不存在");
        }else {
            List<HashMap> result = new ArrayList<>();
            TeacherCourse teacherCourse = teacherCourseDao.getCourseByCourseId(courseId);
            HashMap<String,String > tcMap = new HashMap<>(4);
            tcMap.put("typeContent",teacherCourse.getType().getTypeContent());
            tcMap.put("courseImage",teacherCourse.getCourse().getCourseImage());
            tcMap.put("courseName",teacherCourse.getCourse().getCourseName());
            tcMap.put("csName",teacherCourse.getConsumer().getCsName());
            tcMap.put("courseId", teacherCourse.getCourseId());
            tcMap.put("courseIntro", teacherCourse.getCourse().getCourseIntro());
            result.add(tcMap);
            return ResultInfoUtil.buildSuccess(ResultEnum.OK, "查询课程成功", result);
        }
    }

    /**
     * 设置数据返回格式
     * @param teacherCourseList
     * @return
     */
    public List<HashMap> dataResult(List<TeacherCourse>  teacherCourseList){
        List<HashMap> resultList = new ArrayList<>();
        for (int i = 0; i < teacherCourseList.size() && i < 20; i++) {
            TeacherCourse teacherCourse = teacherCourseList.get(i);
            HashMap<String,Object> courseMap = new HashMap<>(9);
            courseMap.put("courseImage",teacherCourse.getCourse().getCourseImage());
            courseMap.put("courseId",teacherCourse.getCourseId());
            courseMap.put("courseName",teacherCourse.getCourse().getCourseName());
//            courseMap.put("typeId",teacherCourse.getCourse().getTypeId());
            courseMap.put("typeContent",typeDao.getTypeById(teacherCourse.getCourse().getTypeId()).getTypeContent());
//            courseMap.put("typeId",teacherCourse.getCourse().getTypeId());
            courseMap.put("csName",teacherCourse.getConsumer().getCsName());
            courseMap.put("start",teacherCourse.getStart());
            courseMap.put("end",teacherCourse.getEnd());
            courseMap.put("courseInNum",teacherCourse.getCourse().getCourseInNum());
            courseMap.put("now",teacherCourse.getNow());
            courseMap.put("courseIntro",teacherCourse.getCourse().getCourseIntro());
//            courseMap.put("typeContent",teacherCourse.getType().getTypeContent());
            resultList.add(courseMap);
        }
        return resultList;
    }
    @Override
    public ResponseBean getCET46Course() {
        List<TeacherCourse> courseCET4List = teacherCourseDao.getCourseListByTypeContent("四级");
        List<TeacherCourse> courseCET6List = teacherCourseDao.getCourseListByTypeContent("六级");
        List<TeacherCourse> courseList = courseCET4List;
        for (int i = 0; i < courseCET6List.size(); i++){
            courseList.add(courseCET6List.get(i));
        }
        courseList.sort(Comparator.comparing(TeacherCourse::getCourseId));
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"通过类型获取课程列表成功",dataResult(courseList));
    }

    /**
     * 获取图片
     */
    @Override
    public String getImage(String courseId){
        byte[] byteImage = courseDao.getCourseById(courseId).getPhoto();
        String photoType = courseDao.getCourseById(courseId).getPhotoType();
        if(photoType!=null){
            return "data:image/"+photoType+";base64," + Base64.getEncoder().encodeToString(byteImage);
        }else {
            return null;
        }
    }

}
